﻿using System;
using System.Linq;
using Z16.BLI;
using System.Data;

namespace Z16.SITE
{
    public partial class addCadMenu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((Session["CodPerfil"] == null) || (Session["CodUsr"] == null))
            {
                Response.Write("<script>window.location=\'Login.aspx';</script>");
                return;
            }

            if (!IsPostBack)
            {
                this.CarregarTipoMenu();

                var codMenu = Request.QueryString["id"];
                if (codMenu != null && codMenu != "")
                {
                    this.CarregarMenus(int.Parse(codMenu));
                }
            }
        }

        protected void btnGravar_Click(object sender, EventArgs e)
        {
            int? iCodMenu = null;
            int iAtivo = 0;
            Menu menu = new Menu();

            try
            {
                if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
                    iCodMenu = Convert.ToInt32(Request.QueryString["id"]);

                if (chkAtivoMenu.Checked)
                    iAtivo = 1;

                var ret = menu.cadMenu(txtMenu.Text, txtPagina.Text, iAtivo, Convert.ToInt32(ddlTipoMenu.SelectedValue), iCodMenu);
                var retorno = ret.Split(';');

                this.ExibirMensagem(retorno[0], retorno[1]);
            }
            catch
            {
                divError.Visible = true;
            }
        }

        protected void CarregarTipoMenu()
        {
            Menu menu = new Menu();

            ddlTipoMenu.Items.Clear();
            ddlTipoMenu.AppendDataBoundItems = true;
            ddlTipoMenu.Items.Insert(0, "Selecione");

            ddlTipoMenu.DataSource = menu.listarTipoMenu();
            ddlTipoMenu.DataValueField = "codTipoMenu";
            ddlTipoMenu.DataTextField = "strDesTipoMenu";
            ddlTipoMenu.DataBind();
        }

        protected void CarregarMenus(int codMenu)
        {
            Menu menu = new Menu();

            DataTable dt = menu.ListarMenusId(codMenu);
            var cli = dt.AsEnumerable().ToList().First();

            txtMenu.Text = cli[1].ToString();
            txtPagina.Text = cli[2].ToString();
            ddlTipoMenu.SelectedValue = cli[4].ToString();

            if (cli[3].ToString() == "True")
                chkAtivoMenu.Checked = true;
            else
                chkAtivoMenu.Checked = false;
        }

        protected void ExibirMensagem(string erro, string mensagem)
        {
            divSucess.Visible = false;
            divError.Visible = false;

            if (erro.Equals("0"))
            {
                divSucess.Visible = true;
                lblMsg.Text = mensagem;
            }
            else
            {
                divError.Visible = true;
                lblErro.Text = mensagem;
            }
        }
    }
}